package com.zheng.mall.service.impl;

import com.zheng.mall.dto.MemberBrandAttention;
import com.zheng.mall.model.UmsMember;
import com.zheng.mall.repo.MemberBrandAttentionRepository;
import com.zheng.mall.service.MemberAttentionService;
import com.zheng.mall.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * 会员关注Service实现类
 */
@Service
public class MemberAttentionServiceImpl implements MemberAttentionService {
    @Autowired
    private MemberBrandAttentionRepository repository;
    @Autowired
    private UmsMemberService memberService;

    @Override
    public int add(MemberBrandAttention attention) {
        int count = 0;
        UmsMember member = memberService.getCurrentMember();
        attention.setMemberId(member.getId());
        attention.setMemberNickname(member.getNickname());
        attention.setMemberIcon(member.getIcon());
        attention.setCreateTime(member.getCreateTime());
        MemberBrandAttention findAttention = repository.findByMemberIdAndBrandId(attention.getMemberId(), attention.getBrandId());
        if (findAttention != null) {
            repository.save(attention);
            count = 1;
        }
        return count;
    }

    @Override
    public int delete(Long brandId) {
        UmsMember member = memberService.getCurrentMember();
        return repository.deleteByMemberIdAndBrandId(member.getId(),brandId);
    }

    @Override
    public Page<MemberBrandAttention> list(Integer pageNum, Integer pageSize) {
        UmsMember member = memberService.getCurrentMember();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        return repository.findByMemberId(member.getId(),pageable);
    }

    @Override
    public MemberBrandAttention detail(Long brandId) {
        UmsMember member = memberService.getCurrentMember();
        return repository.findByMemberIdAndBrandId(member.getId(), brandId);
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        repository.deleteAllByMemberId(member.getId());
    }
}
